home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / DeveloperLabs / Lab4 / Solution / PaintDocView.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  205 lines

  1. #import "PaintDocView.h"
  2. #import "PaintLabParams.h"
  3.  
  4. #import <appkit/Application.h>
  5. #import <appkit/Bitmap.h>
  6. #import <appkit/Window.h>
  7. #import <dpsclient/wraps.h>
  8.  
  9. @implementation PaintDocView
  10.  
  11.  
  12. +newFrame:(NXRect *)tF
  13. {
  14.     NXRect docRect;
  15.     
  16.     // Create the new view
  17.     self = [super newFrame:tF];
  18.     
  19.     // Initialize parameters
  20.     brushSize = 1;            
  21.     brushShape = ROUND_BRUSH;
  22.     paintColor = NX_BLACK;
  23.     
  24.     // Create and initialize the bitmaps
  25.     paintDropBitmap = [[Bitmap newSize:BITMAP_SIZE :BITMAP_SIZE
  26.                       type:NX_UNIQUEBITMAP] setFlip:NO];
  27.     [self updatePaintDropBitmap];
  28.     docBitmap = [[Bitmap newSize:DOC_WIDTH :DOC_HEIGHT
  29.                 type:NX_NOALPHABITMAP] setFlip:NO];
  30.     NXSetRect(&docRect, 0.0, 0.0, DOC_WIDTH, DOC_HEIGHT);
  31.     [docBitmap lockFocus];
  32.     NXEraseRect(&docRect);
  33.     [docBitmap unlockFocus];
  34.     
  35.     return self;
  36. }
  37.  
  38.  
  39.  
  40.  
  41. - clear
  42. {
  43.     NXRect rect;
  44.     
  45.     // Clear the screen
  46.     [self lockFocus];
  47.     NXEraseRect(&bounds);
  48.     [self unlockFocus];
  49.  
  50.     // Clear the doc bitmap
  51.     NXSetRect(&rect, 0.0, 0.0, DOC_WIDTH, DOC_HEIGHT);
  52.     [docBitmap lockFocus];
  53.     NXEraseRect(&rect);
  54.     [docBitmap unlockFocus];
  55.         return self;
  56. }
  57.  
  58.  
  59.  
  60. - setDocBrushShape:(int)newBrushShape
  61. {
  62.     brushShape = newBrushShape;
  63.     [self updatePaintDropBitmap];
  64.     return self;
  65. }
  66.  
  67.  
  68. - setDocBrushSize:(float)newBrushSize
  69. {
  70.     brushSize = newBrushSize;
  71.     [self updatePaintDropBitmap];
  72.     return self;
  73. }
  74.  
  75. - setDocPaintColor:(float)newPaintColor
  76. {
  77.     paintColor = newPaintColor;
  78.     [self updatePaintDropBitmap];
  79.     return self;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. /*
  86.  * The paintdrop bitmap is a small bitmap which contains the
  87.  * image of a single drop of paint, based on the current color,
  88.  * brush size, and brush shape.
  89.  *
  90.  */
  91.  
  92. - updatePaintDropBitmap
  93. {
  94.     [paintDropBitmap lockFocus];
  95.     PSsetstrokeadjust (NO);
  96.         PScompositerect (0.0, 0.0, BITMAP_SIZE, BITMAP_SIZE, NX_CLEAR);
  97.     PSsetalpha(1.0);       // We want opaque brushes
  98.     PSsetgray(paintColor);
  99.     if (brushShape == SQUARE_BRUSH) {
  100.         PSrectfill (0.0, 0.0, brushSize, brushSize);
  101.         } else if (brushShape == ROUND_BRUSH) {
  102.         PSnewpath();
  103.         PSarc(brushSize/2.0, brushSize/2.0, 
  104.               (brushSize/2.0)-0.5,  // a fudge factor
  105.               0.0, 360.0);
  106.         PSclosepath();    
  107.         PSfill();
  108.     }
  109.     [paintDropBitmap unlockFocus];
  110.  
  111.     return self;
  112. }
  113.  
  114.  
  115.  
  116.  
  117. /*
  118.  * When the mouse button goes down, draw a paintdrop.  Then grab
  119.  * and paint all mouse-dragged events until the button goes up
  120.  * again.
  121.  *
  122.  */
  123.  
  124. #define DRAG_OR_UP_MASK     NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK
  125. - mouseDown:(NXEvent *) theEvent
  126. {
  127.     int old_event_mask;
  128.     NXEvent *latestEvent;
  129.     
  130.     [self paintOneDrop:&theEvent->location];
  131.     old_event_mask = [window eventMask];
  132.     // Enable mouse-dragged and mouse-up events.
  133.     [window addToEventMask:DRAG_OR_UP_MASK];
  134.     while (YES) {
  135.         latestEvent = [NXApp getNextEvent:DRAG_OR_UP_MASK];
  136.         if (latestEvent->type == NX_MOUSEUP) break;
  137.         [self autoscroll:latestEvent];
  138.         [self paintOneDrop:&latestEvent->location];
  139.     }
  140.     // Re-disable mouse-dragged and mouse-up events.
  141.     [window setEventMask:old_event_mask];
  142.         
  143.     return self;
  144. }
  145.  
  146.  
  147.  
  148.  
  149. /*
  150.  * Paints to the view and the docBitmap.  The purpose of the
  151.  * docBitmap is to provide a way for drawSelf:: to know how
  152.  * to repaint broken parts of the screen.
  153.  *
  154.  */
  155.  
  156. - paintOneDrop:(NXPoint *) cursorLocation
  157. {
  158.     NXPoint dest;
  159.     
  160.     // Convert from window's coords to self's.
  161.     [self convertPoint:cursorLocation fromView:nil];
  162.     
  163.     // Center paintdrop on cursor's hotspot.
  164.     dest.x = cursorLocation->x - brushSize/2.0;
  165.     dest.y = cursorLocation->y - brushSize/2.0;
  166.     
  167.     // Draw paintdrop in docView.
  168.     [self lockFocus];
  169.     [paintDropBitmap composite:NX_SOVER toPoint:&dest];
  170.     [[self window] flushWindow];
  171.     [self unlockFocus];
  172.     
  173.     // Draw paintdrop in bitmap.
  174.     [docBitmap lockFocus];
  175.     [paintDropBitmap composite:NX_SOVER toPoint:&dest];
  176.     [docBitmap unlockFocus];
  177.     
  178.     return self;
  179. }
  180.     
  181.  
  182.  
  183. -drawSelf:(NXRect *)r:(int) count
  184. {
  185.     NXPoint dest;
  186.     NXRect source;
  187.     int i;
  188.     
  189.     for (i=((count == 3) ? 1 : 0); i<count; i++) {
  190.         source = *r;
  191.         [docBitmap composite:NX_COPY
  192.                fromRect:&source
  193.                toPoint:&r->origin];
  194.         r++;
  195.     }
  196.  
  197.     return self;
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204. @end
  205.